package sql

import (
	"fmt"
	"game/game-db/orm/meta"
)

type DefaultSql struct {
	tableName    string
	selectSql    string
	selectAllSql string
	insertSql    string
	updateSql    string
	deleteSql    string
	deleteAllSql string
	countAllSql  string

	insertHeadSql string
	insertBodySql string
}

func NewDefaultSql(entityMeta meta.IEntityMeta, tableName string) DefaultSql {
	var sql DefaultSql
	sql.tableName = tableName
	sql.selectSql = buildSelectSql(entityMeta, sql.tableName)
	sql.selectAllSql = buildSelectAllSql(sql.tableName)

	sql.insertHeadSql = buildInsertHeadSql(entityMeta, sql.tableName)
	sql.insertBodySql = buildInsertBodySql(entityMeta)
	sql.insertSql = sql.insertHeadSql + sql.insertBodySql

	sql.updateSql = buildUpdateSql(entityMeta, sql.tableName)

	sql.deleteSql = buildDeleteSql(entityMeta, sql.tableName)
	sql.deleteAllSql = sql.buildDeleteAllSql()
	sql.countAllSql = sql.buildCountAllSql()
	return sql
}

func (t *DefaultSql) buildDeleteAllSql() string {
	return fmt.Sprintf("DELETE FROM `%s`", t.tableName)
}

func (t *DefaultSql) buildCountAllSql() string {
	return fmt.Sprintf("SELECT count(1) AS number FROM `%s`", t.tableName)
}

func (t *DefaultSql) InsertSql() string {
	return t.insertSql
}

func (t *DefaultSql) InsertHeadSql() string {
	return t.insertHeadSql
}

func (t *DefaultSql) InsertBodySql() string {
	return t.insertBodySql
}

func (t *DefaultSql) UpdateSql() string {
	return t.updateSql
}

func (t *DefaultSql) DeleteSql() string {
	return t.deleteSql
}

func (t *DefaultSql) SelectSql() string {
	return t.selectSql
}

func (t *DefaultSql) SelectAllSql() string {
	return t.selectAllSql
}
